const https = require('https');
const express = require('express');
const fs = require('fs');
const path = require('path');
const Config = require("../node_modules_ext/config.js");

const { SOFTWARE } = Config;
 
const SSLStaticServer = {};
console.log(SOFTWARE.appDir)
SSLStaticServer.run = (port) => {
    const KEY_PATH = path.resolve(SOFTWARE.mainDir, './certs/server.key');
    const CRT_PATH = path.resolve(SOFTWARE.mainDir, './certs/server.crt');
    const options = {
        key: fs.readFileSync(KEY_PATH),
        cert: fs.readFileSync(CRT_PATH)
    };
    const app = express();
    app.use(express.static(SOFTWARE.appDir));
    const httpsServer = https.createServer(options, app);
    httpsServer.listen(port);
    console.log('Static服务器正在运行 [端口 - ' + port + ', https]...');
    SSLStaticServer.server = httpsServer;
}

module.exports = SSLStaticServer;
